layui.use(['element', 'table', 'form'], function () {
    table = layui.table;
    $ = layui.jquery;
    layer = layui.layer;
    form = layui.form;

    /* 表格渲染开始 */
    var banner = table.render({
        elem:"#table",
        url:"/banner/getAllBanner",
        toolbar:'#toptool',
        defaultToolbar:['print','filter','exports'],
        page:true,
        limit:5,
        limits:[5,10,15],
        size: 'lg',
        cols:[[
            {type: 'checkbox',width:80},//复选框靠左
            {type:'orderId',title:'序号',align:'center',templet:function (data) {
                    return data.LAY_INDEX;
                }},
            {
                type:'imgAddress',title:'图片',align:'center',templet: function (data) {
                    return "<img src='" + data.imgAddress + "'/>";
                }
            },
            {field:'bannerName',title:'图片名称',align:'center'},
            {field:'trueName',title:'创建人',align:'center'},
            {field:'imgType',title:'图片类型',align:'center',templet:function(data){
                    if(data.imgType == "2"){
                        data.imgType = "商品";
                    }else if(data.imgType == "3"){
                        data.imgType = "视频";
                    }
                    return "<p>"+data.imgType+"</p>";
                }},
            {field:'remarks',title:'备注',align:'center'},
            {field:'updateDate',title:'更新时间',align:'center',templet:function(data){
                    if(data.updateDate == null){
                        return "<p>暂无更新时间</p>"
                    }else{
                        return "<p>"+data.updateDate+"</p>"
                    }
                }
            },
            {
                field: '', title: "操作", align: 'center', templet: function (data) {
                    return "<div>" +
                        "<a href='javascript:getBanner(" + data.carouselId + ");' style='color: #0EAA9E;margin-right: 10px'>编辑</a>" +
                        "<a href='javascript:delProduct(" + data.carouselId + ");' style='color: #0EAA9E'>删除</a>" +
                        "</div>";
                }
            },
        ]],

    });
    /* 表格渲染结束 */

    /*模糊查询*/
    form.on('submit(sub)',function (data) {
        banner.reload({
            url:'/banner/getAllBanner?display='+data.field.display+'&imgType='+data.field.imgType
        });
        return false;
    });
    /* 重置 */
    form.on('submit(subs)',function (data) {
        banner.reload({
            url:'/banner/getAllBanner'
        });
    });


    /* 编辑开始 */
    getBanner = function getBanner(carouselId){
        $.ajax({
            url: "/banner/getBanner",
            type: "post",
            dataType: "json",
            data: {"carouselId": carouselId},
            success: function (data) {
                layer.open({
                    type: 2,
                    title: "编辑",
                    area: ['1000px', '700px'],
                    shade: 0,
                    maxmin:true,
                    offset: 't',
                    content: "/banner/banner-list-update.html",
                    success: function (layero, index) {
                        var obj = layero.find('iframe').contents();
                        obj.find("input[name=bannerName]").val(data.data.bannerName);
                        obj.find("input[name=carouselId]").val(data.data.carouselId);
                        obj.find("input[name=bannerName]").val(data.data.bannerName);
                        var a;
                        if (data.data.imgType == 2) {
                            a = '商品';
                        } else if(data.data.imgType == 3) {
                            a = '视频';
                        }
                        selectLay(obj, data.data.imgType, a, '#imgType');
                        obj.find("input[name=startDate]").val(data.data.startDate);
                        if(data.data.display == 2){
                            radio(obj,"input[name=display]",data.data.display,"上线")
                        }else if(data.data.display == 3){
                            radio(obj,"input[name=display]",data.data.display,'下线')
                        }
                        obj.find("input[name=expireDate]").val(data.data.expireDate);
                        obj.find("#remarks").html(data.data.remarks);
                    }
                });
            },
        });
        //加入lay-value的下拉框矫正
        function selectLay(obj, layValue, value, select) {
            obj.find(select).children().each(function () {
                if ($(this).val() == layValue) {
                    var status = 'dd[lay-value=' + layValue + ']';
                    obj.find(select).siblings("div.layui-form-select").find('dl').find(status).click();
                    $(this).parent().append('<option value="' + layValue + '" selected>' + value + '</option>');
                }
            })
        };
        //单选框矫正
        function radio(obj,input,value,title){
            obj.find(input).each(function () {
               if($(this).val() == value){
                    $(this).parent().children('div').children('div').each(function () {
                        if($(this).text() == title){
                           $(this).prev().click()
                        }
                    })
               }
            });
        }
    }
    /* 编辑结束 */
    delProduct = function delProduct(carouselId){
        layer.confirm("确认删除？",{btn:["确定",'取消'],title:"提示"},function () {
            $.ajax({
                url:'/banner/delBanner',
                data:{"carouselId":carouselId},
                type:'post',
                dataType:'json',
                success:function (data) {
                    layer.msg("删除成功");
                    banner.reload();
                },
            });
        });

    }
});
//刷新
function Refresh() {
    location.reload();
}

//返回
function goBack() {
    parent.$('#beforeHref').val($('.layui-elem-quote').children('span').eq(0).text());
    parent.before();
    return false;
}